<template>
	<view class="page">
		<!-- 用户信息列表 -->
		<view class="user-list">
			<view class="list" style="height: 160rpx;">
				<view class="title">
					<text>头像</text>
				</view>
				<view class="more-content"> 
					<image v-if="userImg" :src="userImg"></image>
					<image v-else :src="userImg"></image> 
					<text class="iconfont icon-more more"></text>
				</view>
			</view>
			<view class="list" @click="onNickname">
				<view class="title">
					<text>昵称</text>
				</view>
				<view class="more-content">
					<text v-if="userName">{{userName}}</text>
					<text v-else>未登录</text>
					<text class="iconfont icon-more more"></text>
				</view>
			</view>
			<view class="list">
				<view class="title">
					<text>性别</text>
				</view>
				<view class="more-content">
					<text class="content">{{sexText}}</text>
					<text class="iconfont icon-more more"></text>
				</view>
				<view class="picker">
					<picker @change="sexPickerChange" :value="sexIndex" :range="sexArray">
						<view class="uni-input" style="height: 100rpx;">{{sexText}}</view>
					</picker>
				</view>
			</view>
			<view class="list">
				<view class="title">
					<text>出生日期</text>
				</view>
				<view class="more-content">
					<text class="content">{{birthday}}</text>
					<text class="iconfont icon-more more"></text>
				</view>
				<view class="picker">
					<picker @change="birthdayPickerChange" mode="date" :value="birthdayDate" :start="startDate" :end="endDate">
						<view class="uni-input" style="height: 100rpx;">{{birthdayDate}}</view>
					</picker>
				</view>
			</view>
		</view>
		<!-- 提示框 -->
		<DialogBox ref="DialogBox"></DialogBox>
	</view>
</template>

<script>
	import user from '../api/user.js';
	export default {
		data() {
			const currentDate = this.getDate({
					format: true
			})
			return {
				// 性别
				sexArray: ['男','女','保密'],
				sexIndex: 0,
				sexText: '保密',
				// 生日
				birthdayDate: currentDate,
				startDate: this.getDate('start'),
				endDate: this.getDate('end'),
				birthday: '2020-02-08',
				DialogBox: {},
				hasToken:false,
				// 昵称
				nickname: '爱跳舞的汤姆猫',
				userName: "", // 新增：用户昵称
				userImg: "https://i.hd-r.icu/8fbedd907a4d948a35f17f102ee89ba1.png", // 新增：用户头像
			};
		},
		onShow() { 
		     const token = uni.getStorageSync('token');
		     if (token) {
		         this.hasToken = true;
		         const userInfo = uni.getStorageSync('usernfo');
				 // 创建 Date 对象
				 const dateObj = new Date(userInfo.birthday); 
				 // 获取年、月、日
				 const year = dateObj.getFullYear();
				 const month = String(dateObj.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始，所以要加 1，并补零
				 const day = String(dateObj.getDate()).padStart(2, '0'); // 补零
				 
				 // 拼接日期字符串
				 var dateStr = `${year}-${month}-${day}`;

		         if (userInfo) {
		             this.userName = userInfo.user_name;
		             this.userImg = userInfo.img;
					 this.sexIndex=userInfo.sex-1;
					 this.sexText = this.sexArray[this.sexIndex];
					 this.birthday=dateStr;
		         }
		     } else {
				 this.sexIndex=2;
		         this.hasToken = false;
		         this.userName = "";
		         this.userImg = "https://i.hd-r.icu/8fbedd907a4d948a35f17f102ee89ba1.png";
		     }
		},
		onLoad() {
		},
		methods:{
			/**
			 * 性别
			 * @param {Object} e
			 */
			sexPickerChange(e){ 
				//0男    1女    2保密
				this.sexIndex = e.detail.value;
				this.sexText = this.sexArray[this.sexIndex]; 
				var token = uni.getStorageSync('token')
				if(token)
				{
						user.userUpdate({
							sex:this.sexIndex+1
						}).then(res=>{
							if(res.code==1)
							{
								uni.showToast({
								      title: res.message,
								      icon: 'success',
								      duration: 1500 // 提示框显示的时间，单位为毫秒，默认 1500
								     });
									var userInfo = uni.getStorageSync('usernfo') 
										userInfo.sex=this.sexIndex+1;
										uni.setStorageSync('usernfo',userInfo)
							}
							 
						})
				}
					 
			},
			/**
			 * 生日
			 * @param {Object} e
			 */
			birthdayPickerChange(e){
				this.birthday = e.detail.value;
				var dateObj = new Date(this.birthday); 
				// 获取时间戳（毫秒）
				var  timestamp = dateObj.getTime();
				console.log(timestamp)
				var token = uni.getStorageSync('token')
				if(token)
				{
				user.userUpdate({
					birthday:timestamp
				}).then(res=>{
					if(res.code==1)
					{
						uni.showToast({
						      title: res.message,
						      icon: 'success',
						      duration: 1500 // 提示框显示的时间，单位为毫秒，默认 1500
						     });
							var userInfo = uni.getStorageSync('usernfo') 
								userInfo.birthday=timestamp;
								uni.setStorageSync('usernfo',userInfo)
					}
				}) 
				}
			},
			/**
			 * 获取日期
			 * @param {Object} type
			 */
			getDate(type) {
				console.log("日期")
				console.log(type)
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
						year = year - 60;
				} else if (type === 'end') {
						year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			/**
			 * 昵称点击
			 */
			onNickname(){
				if(this.hasToken!=false)
				{
					this.$refs['DialogBox'].confirm({
					title: '更改昵称',
					placeholder: '请输入修改的昵称',
					value: this.userName,
					DialogType: 'input',
					animation: 0
				}).then((res)=>{
					if(res.value=="")
					{	
						uni.showToast({
					     title: "昵称不能为空",
					     icon: 'success',
					     duration: 1500 // 提示框显示的时间，单位为毫秒，默认 1500
					    });
					}else{
						const userInfo = uni.getStorageSync('usernfo'); 
					const formData = { 
					    user_name: res.value,
					};  
					 
									user.userUpdate({
										  			user_name: res.value, 
									}).then(res=>{
										  
										              uni.showToast({
													       title: res.message,
													       icon: 'success',
													       duration: 1500 // 提示框显示的时间，单位为毫秒，默认 1500
													      });
										  				userInfo.user_name=formData.user_name
										  				this.userName =formData.user_name;
										  				uni.setStorageSync('usernfo',userInfo) 
									});	
					}
				
				
					 
				})
				}
				
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'Information.scss';
</style>
